package com.game.HUD;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.khronos.opengles.GL10;

import object.obj2D.graphics.Font;
import object.obj2D.graphics.TexSquare;
import object.obj2D.graphics.Texture;
import object.obj2D.weapon.Gun;
import system.Device;
import system.Graphics;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.game.R;

public class ShowGunItem extends ShowGUIItem {
	static byte[] numberFontCharWidth = {9,7,9,9,9,9,8,9,9,9};
	static String numberFontMap = "0123456789";					
	static private Font m_fontNumber;
	static private Font m_fontNumberAlpha;
	
	static private int m_iTexMachineGunIconID;
	static private int m_iTexFireGunIconID;
	static private int m_iTexRocketIconID;
	static private int m_iTexMachineGunIconSelectedID;
	static private int m_iTexFireGunIconSelectedID;
	static private int m_iTexRocketIconSelectedID;
	static private Texture m_texture;
	
	static private TexSquare m_squareGunIcon;
	
	static private int m_iGunIconWidth = 74;
	static private int m_iGunIconHeight = 56;
	static private float m_fSrcWidth;
	static private float m_fSrcHeight;
	static private GL10 gl;
	
	public static void Init()
	{
		m_fSrcWidth = Device.GetWidth();
		m_fSrcHeight = Device.GetHeight();
		gl = Graphics.GetGraphics();
		m_texture = new Texture();
		
		m_iTexMachineGunIconID = m_texture.initTexture(R.drawable.ingame_icon_machinegun);
		m_iTexFireGunIconID = m_texture.initTexture(R.drawable.ingame_icon_firegun);
		m_iTexRocketIconID = m_texture.initTexture(R.drawable.ingame_icon_rocket);
		m_iTexMachineGunIconSelectedID = m_texture.initTexture(R.drawable.ingame_icon_machinegun_selected);
		m_iTexFireGunIconSelectedID = m_texture.initTexture(R.drawable.ingame_icon_firegun_selected);
		m_iTexRocketIconSelectedID = m_texture.initTexture(R.drawable.ingame_icon_rocket_selected);
		
		
		Device.DisableScaleFactor();
		m_squareGunIcon = new TexSquare(m_iGunIconWidth, m_iGunIconHeight);
		Device.EnableScaleFactor();
		
		// Font
		InputStream is = Device.GetContext().getResources().openRawResource(R.drawable.font_number); 
		Bitmap bitmap = BitmapFactory.decodeStream(is);
		try {
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		m_fontNumber = new Font(bitmap, numberFontCharWidth, 9, 2, numberFontMap);
		
		is = Device.GetContext().getResources().openRawResource(R.drawable.font_number_alpha); 
		bitmap = BitmapFactory.decodeStream(is);
		try {
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		m_fontNumberAlpha = new Font(bitmap, numberFontCharWidth, 9, 2, numberFontMap);
	}
	
	public static void Render()
	{
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glPushMatrix();
		
		// Machine gun icon
		gl.glLoadIdentity();
		gl.glTranslatef(-(m_fSrcWidth - m_iGunIconWidth)/2 + 4, -(m_fSrcHeight - m_iGunIconHeight)/2 + 4, 0);
		
		if(s_level.GetPlayer().GetCurrentGun() == Gun.MACHINE_GUN)
			m_texture.loadTexture(m_iTexMachineGunIconSelectedID);
		else 
			m_texture.loadTexture(m_iTexMachineGunIconID);
		
		m_squareGunIcon.Render();
		
		if(s_level.GetPlayer().GetCurrentGun() == Gun.MACHINE_GUN)
			m_fontNumber.drawString(gl, s_level.GetPlayer().GetNumMachineBullet() + "", -5, -15, Font.ALIGN_CENTER);
		else
			m_fontNumberAlpha.drawString(gl, s_level.GetPlayer().GetNumMachineBullet() + "", -5, -15, Font.ALIGN_CENTER);
		
		// Fire gun icon
		gl.glLoadIdentity();
		gl.glTranslatef(-(m_fSrcWidth - m_iGunIconWidth)/2 + 4 + m_iGunIconWidth + 2, -(m_fSrcHeight - m_iGunIconHeight)/2 + 4, 0);
		if(s_level.GetPlayer().GetCurrentGun() == Gun.FIRE_GUN)
			m_texture.loadTexture(m_iTexFireGunIconSelectedID);
		else
			m_texture.loadTexture(m_iTexFireGunIconID);
		
		m_squareGunIcon.Render();
		
		if(s_level.GetPlayer().GetCurrentGun() == Gun.FIRE_GUN)
			m_fontNumber.drawString(gl, s_level.GetPlayer().GetNumFireBullet() + "", -5, -15, Font.ALIGN_CENTER);
		else
			m_fontNumberAlpha.drawString(gl, s_level.GetPlayer().GetNumFireBullet() + "", -5, -15, Font.ALIGN_CENTER);
		
		
		// Rocket icon
		gl.glLoadIdentity();
		gl.glTranslatef(-(m_fSrcWidth - m_iGunIconWidth)/2 + 4 + (m_iGunIconWidth + 2) * 2, -(m_fSrcHeight - m_iGunIconHeight)/2 + 4, 0);
		m_texture.loadTexture(m_iTexRocketIconSelectedID);
		m_squareGunIcon.Render();
		
		m_fontNumber.drawString(gl, s_level.GetPlayer().GetiNumMissile() + "", -5, -15, Font.ALIGN_CENTER);
		
		gl.glPopMatrix();
	}
}
